//
//  NSDictionary+JKNullReplacer.m
//  JKNullReplacer
//
//  Created by Jobin Kurian on 14/03/14.
//  Copyright (c) 2014 Jobin Kurian. All rights reserved.
//

#import "NSDictionary+JKNullReplacer.h"
#import "NSArray+JKNullReplacer.h"

@implementation NSDictionary (JKNullReplacer)

- (NSDictionary *)dictionaryWithoutNulls
{
    const NSMutableDictionary *replacedDictionary = [self mutableCopy];
    const id nullValue = [NSNull null];
    const NSString *blankString = nil;
    
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        id object = [self objectForKey:key];
        
        if (object == nullValue)
//            [replacedDictionary setObject:blankString forKey:key];
            [replacedDictionary removeObjectForKey:key];
        
        else if ([object isKindOfClass:[NSDictionary class]])
            [replacedDictionary setObject:[object dictionaryWithoutNulls] forKey:key];
        
        else if ([object isKindOfClass:[NSArray class]])
            [replacedDictionary setObject:[object arrayWithoutNulls] forKey:key];
    }];
    
    return [NSDictionary dictionaryWithDictionary:[replacedDictionary copy]];
}

- (BOOL)server_Getcodecheck
{
    
    NSString *success = IF_NULL_TO_STRING([self objectForKey:@"success"]);
    NSString *code    = IF_NULL_TO_STRING([self objectForKey:@"code"]);
    
    
    return [success isEqualToString:@"1"]&&[code isEqualToString:@"200"]?YES:NO;
    
}

- (nullable id)safeObjectForKey:(nullable NSString *)key {
    id value = [self objectForKey:key];
    if ([value isEqual:[NSNull null]]) {
        value = nil;
    }
    return value;
}


//将NSDictionary中的Null类型的项目转化成@""
+(NSDictionary *)nullDic:(NSDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [myDic objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

//将NSDictionary中的Null类型的项目转化成@""
+(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
    }
    return resArr;
}

//将NSString类型的原路返回
+(NSString *)stringToString:(NSString *)string
{
    return string;
}

//将Null类型的项目转化成@""
+(NSString *)nullToString
{
    return @"";
}

#pragma mark - 公有方法
//类型识别:将所有的NSNull类型转化成@""
+ (id)changeType:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
    {
        return [self nullDic:myObj];
    }
    else if([myObj isKindOfClass:[NSArray class]])
    {
        return [self nullArr:myObj];
    }
    else if([myObj isKindOfClass:[NSString class]])
    {
        return [self stringToString:myObj];
    }
    else if([myObj isKindOfClass:[NSNull class]])
    {
        return [self nullToString];
    }
    else
    {
        return myObj;
    }
}



@end
